#!/bin/bash
# Copyright (C) 2024-2025 KoudaiPC Computer Technologies Co., Ltd.
# script name: prepare-upgrade-tgz.sh
# author: david921518@qq.com

################
# global vars
GLB_WORK_DIR=
################

user=$(whoami)

log_kmsg_path="/dev/kmsg"
log_con_path="/dev/tty1"

# 定义日志格式-前缀
log_prefix="[prepare-upgrade-tgz]"
# 定义日志目录和文件名
if [ "$user" == "root" ]; then
  log_file_dir="/var/log/kpc/huangdi"
  log_kmsg_path="/dev/null"
else
  log_file_dir=~/log/kpc/huangdi
  log_kmsg_path="/dev/null"
fi
log_file_path="$log_file_dir/host-build.log"

function log_init()
{
  # 文件不存在则创建
  if [ ! -d "$log_file_dir" ]; then
    mkdir -p $log_file_dir
    chmod -R 755 $log_file_dir
  fi
  if [ ! -f "$log_file_path" ]; then
    touch $log_file_path
  fi
}

# 定义日志打印格式
function log()
{
  # 定义日志时间格式
  local curtime=$(date "+%Y-%m-%d %H:%M:%S")
  echo "$curtime $log_prefix $@"
  echo "$curtime $log_prefix $@" >>$log_file_path
  if [ "$user" == "root" ]; then
    echo "$log_prefix [kmsg] $@" >$log_kmsg_path
    #sleep 0.1
    #echo "$curtime $log_prefix [con] $@" >$log_con_path
    #sleep 0.1
  fi
}

function log_from_file()
{
  local tmpfile=$@
  if [ ! -f "$tmpfile" ]; then
    log "no file [$tmpfile] to log"
    return 1
  fi

  local n=0
  local line=""
  local -a lists=()
  while read line;
  do
    lists[$n]="$line"
    ((n++))
  done <$tmpfile

  # 定义日志时间格式
  local curtime=$(date "+%Y-%m-%d %H:%M:%S")

  #echo "$curtime $log_prefix $@"
  #echo "$curtime $log_prefix $@" >>$log_file_path
  local i=0
  for i in $( seq 0 $((${#lists[*]} - 1)) )
  do
    echo "$curtime $log_prefix ${lists[i]}"
    echo "$curtime $log_prefix ${lists[i]}" >>$log_file_path
  done

  if [ "$user" == "root" ]; then
    #echo "$log_prefix [kmsg] $@" >$log_kmsg_path
    for i in $( seq 0 $((${#lists[*]} - 1)) )
    do
      echo "$curtime $log_prefix ${lists[i]}" >$log_kmsg_path
      [ $((i%10)) -ne 9 ] || sleep 0.1
    done

    #sleep 0.1

    #echo "$curtime $log_prefix [con] $@" >$log_con_path
    #for i in $( seq 0 $((${#lists[*]} - 1)) )
    #do
    #  echo "$curtime $log_prefix ${lists[i]}" >$log_con_path
    #done

    #sleep 0.1
  fi
}

function get_work_dir()
{
  ################
  # /kpcos/distro/huangdi/tools/
  # /kpcos/distro/huangdi/releases/1.0/targets/x86/64/host/huangdi-host-setup-nocloud-amd64-image.sh
  ################

  local real_path=$(realpath $0)
  local real_dir=$(dirname "$real_path")
  GLB_WORK_DIR=$(realpath "$real_dir/../releases/")
  log "WORK DIR is [$GLB_WORK_DIR]"
}

target_list=( "x86/64" "armsr/armv8" )
myver_list=(
  "0.56" "1.04" "1.5"
)
ver_list=(
  "0.5"  "0.51" "0.52" "0.53" "0.54" "0.55" "0.56"
  "1.0"  "1.01" "1.02" "1.03" "1.04"
  "1.5"
)

# 0. init scripts
log_init
get_work_dir
cd "$GLB_WORK_DIR"

# 1. Make work dir
for ver in "${myver_list[@]}"
do
  for target in "${target_list[@]}"
  do
    host_dir="$GLB_WORK_DIR"/$ver/targets/$target/host
    up_file="$host_dir"/huangdi-host-prepare-upgrade-tgz.sh
    log "host_dir=[$host_dir]"
    log "up_file=[$up_file]"

    if [ ! -d $host_dir ]; then
      log "no dir [$host_dir], continue..."
      continue
    fi

    log "cd $host_dir"
    cd "$host_dir"

    log "(\"$up_file\")"
    ("$up_file")
  done
done

cd "$GLB_WORK_DIR"

exit 0
